#include "stdafx.h"
#include "Label.h"
#include "ColorDef.h"
#include "LabelDialog.h"


Label::Label(void)
{
	text_ = L"NO";
}


Label::Label(CWnd* pParent)
	: Graphic(pParent)
{
	text_ = L"NO";
	labelData_.color_ = COLOR_WHITE;
	font_.Attach(GetStockObject(SYSTEM_FONT));
	font_.GetLogFont(&labelData_.logFont_);
}


Label::~Label(void)
{
}


IMPLEMENT_SERIAL(Label, Graphic, 1)


void Label::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		ar.Write(&labelData_, sizeof(labelData_));
		ar << text_;
	}
	else
	{	
		ar.Read(&labelData_, sizeof(labelData_));
		ar >> text_;

		font_.CreateFontIndirect(&labelData_.logFont_);
	}
}


BEGIN_MESSAGE_MAP(Label, Graphic)
	ON_WM_PAINT()
	ON_WM_WINDOWPOSCHANGED()
END_MESSAGE_MAP()


void Label::OnPaint()
{
	CPaintDC dc(this); 
	dc.SetBkMode(TRANSPARENT);
	dc.SelectObject(&font_);
	dc.SetTextColor(labelData_.color_);
	dc.TextOutW(0, 0, text_);
}


void Label::setWindow(const CPoint& pt1, const CPoint& pt2)
{
	labelData_.pt_ = pt1;
	setWindow();
}


void Label::setWindow()
{
	CClientDC dc(this);

	dc.SelectObject(&font_);
	CSize textSize = dc.GetTextExtent(text_);
	CRect wndRect(labelData_.pt_, (labelData_.pt_ + textSize));
	MoveWindow(wndRect);
}


bool Label::setAttr(void)
{
	LabelDialog dlg(GetParent());
	dlg.setLabel(this);
	return dlg.DoModal() == IDOK;
}


void Label::OnWindowPosChanged(WINDOWPOS* lpwndpos)
{
	Graphic::OnWindowPosChanged(lpwndpos);

	CPoint pt(lpwndpos->x, lpwndpos->y);
	labelData_.pt_ = pt;
}
